package com.yangle.frame.common.utils;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUtils {
    public static void uploadFile(@RequestParam("file") MultipartFile file, String filePath) throws IOException {
        byte[] bytes = file.getBytes();
        File file1 =new File(filePath);
        if(!file1.getParentFile().exists()){
            file1.getParentFile().mkdirs();
        }
        if(!file1.exists()){
            file1.createNewFile();
        }
        Path path = Paths.get(filePath);
        if(!Files.exists(path)){

            new File(filePath).mkdirs();
        }
        Files.write(path, bytes);
        FileCopyUtils.copy(bytes,file1);
    }
}